<template>
  <a-modal
    :title="title"
    :visible="open"
    :confirm-loading="submitLoading"
    @cancel="handleClose"
    @ok="handleSubmit"
    :destroyOnClose="true"
    width="800px">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="生成类型" prop="type">
        <a-radio-group v-model="form.type" :default-value="form.type" button-style="solid">
          <a-radio-button :value="1">生成到默认路径</a-radio-button>
          <a-radio-button :value="2">生成到项目</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="默认路径" prop="defaultPath" v-if="form.type == 1">
        <a-input v-model="form.defaultPath" placeholder="请输入默认路径"/>
      </a-form-model-item>
      <a-form-model-item label="服务端项目路径" prop="servicePath" v-if="form.type == 2">
        <a-input v-model="form.servicePath" placeholder="请输入服务端项目路径"/>
      </a-form-model-item>
      <a-form-model-item label="PC端项目路径" prop="pcPath" v-if="form.type == 2">
        <a-input v-model="form.pcPath" placeholder="请输入PC端项目路径"/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import {generateGenTable, getPathConfig} from '@/api/generate/genTable'

export default {
  data() {
    return {
      submitLoading: false,
      title: '代码生成',
      tableIdList: [],
      // 表单
      form: {
        type: 1,
        defaultPath: '',
        servicePath: '',
        pcPath: ''
      },
      open: false,
      rules: {
        type: [{required: true, trigger: ['blur', 'change'], message: '请选择生成类型'}],
        defaultPath: [{required: true, trigger: ['blur', 'change'], message: '请输入默认路径'}],
        servicePath: [{required: true, trigger: ['blur', 'change'], message: '请输入服务端项目路径'}],
        pcPath: [{required: true, trigger: ['blur', 'change'], message: '请输入PC端项目路径'}]
      }
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.$refs.formRef.resetFields()
    },
    /** 点击生成 */
    handleGenerate(id) {
      this.tableIdList = [id]
      getPathConfig().then(res => {
        this.form = res.data
      })
      this.open = true
    },
    /** 点击批量生成 */
    handleGenerateBatch(ids) {
      this.tableIdList = ids
      getPathConfig().then(res => {
        this.form = res.data
      })
      this.open = true
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          this.form.tableIdList = this.tableIdList
          generateGenTable(this.form).then(res => {
            this.$message.success(res.msg, 3)
            this.handleClose()
            this.$emit('ok')
          }).finally(() => {
            this.submitLoading = false
          })
        }
      })
    }
  }
}
</script>
